package com.qyi.activemq.topic.producer;

import com.qyi.activemq.topic.ActiveMQProperties;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.JMSConnectionFactory;

/**
 * @author luqi
 * @Date 2021/3/11 14:18
 * @Description TODO
 */
@Configuration
public class MqProductConfig {


    public ActiveMQConnectionFactory activeMQConnectionFactory(ActiveMQProperties properties) {
        ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory(
                        properties.getUser(),
                        properties.getPassword(),
                        properties.getBrokerUrl());
        return activeMQConnectionFactory;
    }

    @Bean(name = "producerConnection")
    JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties) {
        ActiveMQConnectionFactory connectionFactory = (activeMQConnectionFactory(properties));
        JmsPoolConnectionFactory pooledConnectionFactory = (new JmsPoolConnectionFactoryFactory(properties.getPool())).createPooledConnectionFactory(connectionFactory);
        return pooledConnectionFactory;
    }

    @Bean
    public JmsMessagingTemplate jmsMessagingTemplate(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        return new JmsMessagingTemplate(jmsTemplate);
    }


}
